<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<link href="./js/calendar/css/jscal2.css" rel="stylesheet" type="text/css">
<link href="./js/calendar/css/border-radius.css" rel="stylesheet" type="text/css">
<link href="./js/calendar/css/gold/gold.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="./js/calendar/js/jscal2.js"></script>
<script type="text/javascript" src="./js/calendar/js/lang/en.js"></script>

<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript"><!--

function checkForm(){
	var grossBackCostIds = document.getElementsByName("grossBackCostId");
	var costs = document.getElementsByName("cost");
	var backCostTypeIds = document.getElementsByName("backCostTypeId");
	var backCostWayIds = document.getElementsByName("backCostWayId");
	var backDates = document.getElementsByName("backDate");
	var backCostDescs = document.getElementsByName("backCostDesc");
	var salePersons = document.getElementsByName("salePerson");
	
	var sign = 0;

	return true;
}

function changeCheckbox(sindex,index){
	var selectValue = document.getElementsByName("grossBackCostId")[sindex].value;
	if(selectValue == '2'){
		document.getElementsByName("businessCheckbox")[index].disabled = false;
		document.getElementsByName("businessCheckbox")[index].checked = true;
		document.getElementsByName("businessStatus")[index].value = "1";
	}else{
		document.getElementsByName("businessCheckbox")[index].disabled = true;
		document.getElementsByName("businessCheckbox")[index].checked = false;
		document.getElementsByName("businessStatus")[index].value = "0";
	}
}

function changeValue(index){
	var businessCheckbox = document.getElementsByName("businessCheckbox")[index];
	if(businessCheckbox.checked){
		document.getElementsByName("businessStatus")[index].value = "1";
	}else{
		document.getElementsByName("businessStatus")[index].value = "0";
	}
}

function back(){
	var uri = "./contractBackCost.do";
	var sign = "<c:out value='${sign}'/>";
	if(sign == "1"){//返回到合同应收帐管理页面
		uri = "./contractBackCost.do";
	}else if(sign == "2"){//返回到部门合同应收帐管理页面
		uri = "./contractBackCost.do?action=deptBackCostManage";
	}else if(sign == "3"){//返回到部门所有合同页面
		uri = "./contract.do?action=preDeptContract";
	}else if(sign == "4"){//返回到我的历史合同页面
		uri = "./contract.do?action=preClosedContract";
	}
	window.location.href = uri;
	window.location.target = "main";
}

function personDialog(rowNum){
	if(confirm("请注意，修改销售代表将会导致业绩考核发生变动！")){
		var url = "./contract.do?action=selectSalePerson";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
			var salePersonId = "";
			var salePerson = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				salePersonId = tStr[0];
				salePerson = tStr[1];
			}
			document.getElementsByName("salePersonId")[Number(rowNum) ].value = salePersonId;
			document.getElementsByName("salePerson")[Number(rowNum) ].value = salePerson;
			/**document.getElementById("salePersonId").value = salePersonId;										
			document.getElementById("salePerson").value = salePerson;
			*/
		}
	}
}

function save(){
	if(checkForm()){
		document.updateBackCostForm.action = "./contractBackCost.do?action=updateBackCostNew";
		document.updateBackCostForm.target = "main";
		document.updateBackCostForm.submit();
	}
}


function initPage(){
	/**if("<c:out value='${result}'/>" == 1){
		alert("<fmt:message key='common.message.addSuccess'/>");
	}*/
}

function calculateCost(obj,totalValue)
{
	if(obj.value > totalValue)
	{
		alert("不能超过总数");
		return false;
	}
	
	var officialCostAry = document.getElementsByName("officialCost");
	var operationCostAry = document.getElementsByName("operationCost");
	var proxyCostAry = document.getElementsByName("proxyCost");
	
	var officialTotal = 0.0;
	var operationTotal = 0.0;
	var proxyTotal = 0.0;
	for(var i=0; i<officialCostAry.length; i++)
	{
		if(!officialCostAry[i].value)
			officialTotal += 0.0;
		else
			officialTotal += parseFloat(officialCostAry[i].value);
	}
	for(var i=0; i<operationCostAry.length; i++)
	{
		if(!operationCostAry[i].value)
			operationTotal += 0.0;
		else
			operationTotal += parseFloat(operationCostAry[i].value);
	}
	for(var i=0; i<proxyCostAry.length; i++)
	{
		if(!proxyCostAry[i].value)
			proxyTotal += 0.0;
		else
			proxyTotal += parseFloat(proxyCostAry[i].value);
	}	
	
	document.getElementById("costSubTotal").value = parseFloat(officialTotal) + parseFloat(operationTotal) + parseFloat(proxyTotal);
	document.getElementById("costRemain").value = document.getElementById("costRemain").value - (parseFloat(officialTotal) + parseFloat(operationTotal) + parseFloat(proxyTotal));
}


--></script>
</head>
<body onload="initPage()">
<form name="updateBackCostForm" action="" method="post">
<input id="id" name="id" type="hidden" value="${backCostNew.id}"/>
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">新增回款</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<button class="btn_submit" onclick="save()" hidefocus="true">保存</button>&nbsp;&nbsp;
							<button class="btn_back" onclick="back()" hidefocus="true">放弃</button>
							<button class="btn_submit" onclick="back()" hidefocus="true">全额回款</button>
							<button class="btn_back" onclick="back()" hidefocus="true">清除</button>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_title2">
								<tr>
									<td class="title">回款方式</td>
									<td>
										<select id="costTypeId" name="costTypeId">
											<c:forEach var="range" items="${ranges['36']}">
												<option value="${range.id}" <c:if test="${range.id == backCostNew.backCostTypeId}">selected</c:if> >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>																				
									<td class="title">单据号</td>
									<td>
										<input id="costNum" name="costNum" type="text" value="${backCostNew.backCostNum}"/>
									</td>										
								</tr>
								<tr>
									<td class="title">回款日期</td>
									<td colspan="3">	
										<input id="backDate" name="backDate" class="input_long" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" value="${backCostNew.backDateStr}"/>
									</td>										
								</tr>								
								<tr>
									<td class="title">备注</td>
									<td colspan="3">
										<input id="backCostDesc" name="backCostDesc" type="text" value="${backCostNew.backCostDesc}"/>
									</td>									
								</tr>			
								<tr>
									<td class="title">应收帐款余额</td>
									<td>
										<input id="costRemain" name="costRemain" type="text" value="${caseRemain}" readonly/>
									</td>										
									<td class="title">本次回款金额合计</td>
									<td>
										<input id="costSubTotal" name="costSubTotal" type="text" value="${backCostNew.backCostTotal}" readonly/>
									</td>										
								</tr>																																			
							</table>
						</td>
					</tr>
					<tr><td>&nbsp;</td></tr>
					<tr>
						<td>
							<table id="table1" class="table_style">
								<tr>
									<td class="title">商品名称</td>
									<td class="title">官费</td>
									<td class="title">业务费用</td>
									<td class="title">代理费</td>
								</tr>							
								<c:forEach var="backCase" items="${backCostNew.backCostHistoryList}" varStatus="bc">
									<tr>
										<td>
											<input id="contractCaseId" name="contractCaseId" type="hidden" value="${backCase.contractCaseId}"/>
											<input id='contractCaseName' name='contractCaseName' class='input_percent' style='width:300px' type='text' value='${backCase.contractCase.caseCode}' readonly/>
										</td>
										<td>
											<input class='input_percent' style='width:70px; background-color:gray' type='text' value='${backCase.contractCase.officialCost}' readonly/>
											&nbsp;
											<input id='officialCost' name='officialCost' class='input_percent' style='width:100px' type='text' value='${backCase.officialCost}' onChange="javascript:calculateCost(this,${backCase.contractCase.officialCost})"/>
										</td>
										<td>
											<input class='input_percent' style='width:70px; background-color:gray' type='text' value='${backCase.contractCase.operationCost}' readonly/>
											&nbsp;
											<input id='operationCost' name='operationCost' class='input_percent' style='width:100px' type='text' value='${backCase.operationCost}' onChange="javascript:calculateCost(this,${backCase.contractCase.operationCost})"/>
										</td>
										<td>
											<input class='input_percent' style='width:70px; background-color:gray' type='text' value='${backCase.contractCase.proxyCost}' readonly/>
											&nbsp;
											<input id='proxyCost' name='proxyCost' class='input_percent' style='width:100px' type='text' value='${backCase.proxyCost}' onChange="javascript:calculateCost(this,${backCase.contractCase.proxyCost})"/>
										</td>
									</tr>
								</c:forEach>
							</table>
						</td>
					</tr>
				</table>			
			</td>
		</tr>
	</table>
	<input id="contractId" name="contractId" type="hidden" value="${contractId}"/>
	<input id="sign" name="sign" type="hidden" value="${sign}"/>
</form>	 
</body>
</html>